package com.css.security.app.controller;

import com.css.security.app.social.AppSingUpUtils;
import com.css.common.security.constant.SecurityConstants;
import com.css.security.core.social.SocialController;
import com.css.common.security.support.SocialUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * AppSecurityController
 *
 * @author hanyx
 * @date 2019/04/17
 */
@RestController
public class AppSecurityController extends SocialController {


    @Autowired
    private AppSingUpUtils appSingUpUtils;

    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    /**
     * 用户第一次社交登录时，会引导用户进行用户注册或绑定
     * 此服务用于在注册或绑定页面获取社交网站用户信息
     *
     * @param request
     * @return
     */
    @GetMapping(SecurityConstants.DEFAULT_SOCIAL_USER_INFO_URL)
    public SocialUserInfo getSocialUserInfo(HttpServletRequest request) {
        Connection<?> connection = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
        appSingUpUtils.saveConnectionData(new ServletWebRequest(request), connection.createData());
        return buildSocialUserInfo(connection);
    }

}
